const mongoose = require("mongoose");
const dayjs = require("dayjs");
const md5 = require("md5");

const userSchema = new mongoose.Schema({
    username: { type: String, required: true, unique: true },
    password: { type: String, required: true, select: false },
    avatar: { type: String, default: "" },
    status: { type: Number, default: 0, enum: [0, 1], select: false },
    role: { type: String, default: "user", enum: ["user", "admin"] },
    createdAt: { type: String, select: false },
    lastLogin: { type: String },
});

userSchema.pre("save", function (next) {
    this.password = md5(this.password);
    this.createdAt = dayjs().format("YYYY/MM/DD HH:mm:ss");
    this.lastLogin = dayjs().format("YYYY/MM/DD HH:mm:ss");
    next();
});
module.exports = mongoose.model("User", userSchema);
